import time
import cv2, numpy as np
from findimage import find_template, find_all_template
WIDTH_WIN = 1105
HEIGHT_WIN = int(0.5625*WIDTH_WIN) # 宽高比16:9
delta_X = 1
delta_Y	= 66
scaR = WIDTH_WIN/865
Y_list = list(range(int(19*scaR), int(950*scaR), int(32.5*scaR)))
image_origin = cv2.resize(cv2.imread('./Pics/MapPanl.png'), None, fx=WIDTH_WIN/1362, fy=WIDTH_WIN/1362, interpolation=cv2.INTER_AREA)
image_template = cv2.imread('imgTest.png')
#print("RED:",image_origin[69, 391])
#print("Manhattan Distance: ",np.linalg.norm(np.array([24,25,192])-image_origin[69,391]))
#boss_status = find_template(image_origin[45:115, 80:863],image_template)
#print(boss_status, np.array([80, 45]) + np.array(boss_status["result"]))
#h, w = image_origin.shape[:-1] # 取得截图宽度和高度
#print(f'截图宽度:{w},高:{h}')
#center_point = np.array([491, 264]) # 坐标旋转圆心
#coords = []
#theta = np.radians(45) #角度转弧度
#rotation_matrix = np.array([[np.cos(theta), np.sin(theta)], [-np.sin(theta), np.cos(theta)]]) #顺时针
#rotation_matrix = np.array([[np.cos(theta), -np.sin(theta)], [np.sin(theta), np.cos(theta)]]) #逆时针
	
start_time = time.time()
match_result = find_template(image_origin, image_template[int(0.162*HEIGHT_WIN + delta_Y):int(0.724*HEIGHT_WIN + delta_Y), int(0.11*WIDTH_WIN + delta_X):int(0.216*WIDTH_WIN + delta_X)])
Y_m = int(0.443*HEIGHT_WIN + (659*scaR-match_result['result'][1]) + delta_Y)
print(Y_m)
# 查找所有匹配
#match_results = find_all_template(image_origin, image_template, 0.7, 9)
'''
for match_result in match_results:
	original_point = np.array(match_result["result"])
	rotated_point = np.dot(rotation_matrix, original_point - center_point) + center_point
	#if ((rotated_point[0]/w < 0.7) and 0.15 < rotated_point[1]/h < 0.4):
	coords += [(match_result["result"]),tuple(rotated_point), match_result["confidence"]]#, np.linalg.norm(original_point - zero_point, ord=1))]
print("total time: {}".format(time.time() - start_time))
print(coords) '''
# 绘制结果图
img_result_0 = image_template.copy()
img_result_1 = image_origin.copy()
img_result_2 = image_template[int(0.162*HEIGHT_WIN + delta_Y):int(0.724*HEIGHT_WIN + delta_Y), int(0.11*WIDTH_WIN + delta_X):int(0.216*WIDTH_WIN + delta_X)].copy()
#for match_result in match_results:
rect = match_result['rectangle']
cv2.rectangle(img_result_0, (95, Y_m-5), (232, Y_m+5), (0, 0, 220), 2)
cv2.rectangle(img_result_1, (rect[0][0], rect[0][1]), (rect[3][0], rect[3][1]), (0, 0, 220), 2)
cv2.imwrite('img_result_0.png', img_result_0)
cv2.imwrite('img_result_1.png', img_result_1)
cv2.imwrite('img_result_2.png', img_result_2)